package com.app.qbake.module.square.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hannesdorfmann.adapterdelegates3.AdapterDelegate;
import com.wind.base.adapter.DisplayItem;
import com.wind.data.square.bean.Goods;
import com.wind.data.square.bean.Pic;
import com.wind.data.square.bean.SquareItem;
import com.app.qbake.R;
import com.app.qbake.module.square.activity.GoodsDetailActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wind on 2018/9/6.
 */

public class SellerGoodsDelegate extends AdapterDelegate<List<DisplayItem>> {
    private Activity mActivity;
    public SellerGoodsDelegate(Activity activity){
        mActivity=activity;
    }
    @Override
    protected boolean isForViewType(@NonNull List<DisplayItem> items, int position) {
        return items.get(position) instanceof SquareItem;
    }

    @NonNull
    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view=mActivity.getLayoutInflater().inflate(R.layout.item_square,parent,false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull List<DisplayItem> items, int position, @NonNull RecyclerView.ViewHolder holder, @NonNull List<Object> payloads) {
        final SquareItem item= (SquareItem) items.get(position);
        ViewHolder vh= (ViewHolder) holder;
        Glide.with(mActivity)
                .load(item.getAvatar())
                .into(vh.iv_avatar);

        vh.tv_name.setText(item.getUserName());
        vh.tv_time.setText(item.getUpdated_at());
        vh.tv_desc.setText(item.getTitle());
        vh.tv_price.setText("￥"+item.getPrice());

        PicAdapter picAdapter=new PicAdapter(mActivity,R.layout.item_pic);
        picAdapter.setOnItemClickListener(new PicAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Pic pic, int position) {
                GoodsDetailActivity.start(mActivity,item.getId());
            }
        });
        List<Pic> pics=new ArrayList<>();
        if (item.getIs_video()== Goods.CODE_YES && !TextUtils.isEmpty(item.getVideo_image())){
            Pic video=new Pic(item.getVideo_image());
            video.setVideoCover(true);
            pics.add(video);
        }
        for (String url:item.getImages_url()){
            pics.add(new Pic(url));
            if (pics.size()==3){
                break;
            }
        }
        picAdapter.replace(pics);
        int spanCount=3;
        if (pics.size()<3){
            spanCount=2;
        }
        GridLayoutManager gridLayoutManager=new GridLayoutManager(mActivity,spanCount);
        vh.rv_pic.setLayoutManager(gridLayoutManager);
        vh.rv_pic.setAdapter(picAdapter);

     /*   vh.rv_pic.addOnItemTouchListener(new RecyclerViewItemTouchListener(vh.rv_pic, new RecyclerViewItemTouchListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view) {

            }

            @Override
            public void onItemLongClick(View view) {

            }
        }));*/
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GoodsDetailActivity.start(mActivity,item.getId());
            }
        });
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.iv_avatar)
        ImageView iv_avatar;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_price)
        TextView tv_price;
        @BindView(R.id.rv_pic)
        RecyclerView rv_pic;
        @BindView(R.id.tv_desc)
        TextView tv_desc;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
